//
//  WBNavigationController.swift
//  Weibo
//
//  Created by niqiang on 16/6/30.
//  Copyright © 2016年 niqiang. All rights reserved.
//

import UIKit

class WBNavigationController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()
        navigationBar.isHidden = true
    }
    
  //重写push方法,所有push动作调用
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        if childViewControllers.count > 0 {
            //隐藏底部Tabbar
            viewController.hidesBottomBarWhenPushed = true
        //判断控制器的类型
            if let vc = viewController as? WBBaseViewController {
                var title = "返回"
                
                //判断控制器的级数，只有一个控制器的时候，显示栈地控制器的标题
                if childViewControllers.count == 1 {
                    //title显示首页额标题
                    title = childViewControllers.first?.title ?? "返回"
                }
                //取出自定义的navItem
                vc.navItem.leftBarButtonItem = UIBarButtonItem(title: title, target: self, action: #selector(popToParent), isBack: true)
            }
        }
        
        super.pushViewController(viewController, animated: true)
    }
    
    //pop 返回到上一级控制器
    @objc private func popToParent(){
    popViewController(animated: true)
    }
    
}
